/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.model.Patient;
import healthcare.coursework.model.TestSamples;
import healthcare.coursework.model.TestSamplesPK;
import healthcare.coursework.service.AppointmentService;
import healthcare.coursework.service.PatientService;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zkplus.databind.BindingListModelList;

import org.zkoss.zul.Button;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Div;
import org.zkoss.zul.Groupbox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author Administrator
 */
public class TrackSamples extends GenericAutowireComposer {

    //Componennt
    private Combobox trackCodeSearch;
    private Button patientQuery;
    private Listbox sampleDataListView;
    private Textbox result;
    private Div sampleReuslt;
    //Service
    private AppointmentService sampleService = util.Utility.getUtility().getAppointmentService();
    private PatientService patientService = util.Utility.getUtility().getPatientService();
    private List<TestSamples> samples;
    private Groupbox gbUtility;

    private void setInfo() {
        trackCodeSearch.setModel(new ListModelList(patientService.findPatients(), true));
        doFilter();
        sampleReuslt.setVisible(false);
    }

    public void onPatientQuery(Event event) {
        // System.out.println("Test Samples");
        doFilter();
    }

    public void onLoadList(Event event) {
        setInfo();
    }

    public void onResult(Event event) {
        try {
            TestSamplesPK id = (TestSamplesPK) sampleDataListView.getSelectedItem().getValue();
            TestSamples ts = sampleService.findTestSample(id);
            sampleReuslt.setVisible(true);
            result.setValue(ts.getSampleResult());
        } catch (Exception e) {
            sampleReuslt.setVisible(false);
        }

    }

    private void doFilter() {
        try {
            System.out.println(trackCodeSearch.getText());
            if (trackCodeSearch.getText() != null || !trackCodeSearch.getText().trim().equals("")) {
                samples = sampleService.listOfAdmittedLabTesting((String) trackCodeSearch.getText());
                System.out.println("Samples");
            } else {
                samples = sampleService.listOfAdmittedLabTesting();
            }
        } catch (Exception e) {
            samples = sampleService.listOfAdmittedLabTesting();
        }
        if (!samples.isEmpty()) {
            gbUtility.setVisible(true);
            sampleDataListView.setModel(new BindingListModelList(samples, true));
        } else {
            gbUtility.setVisible(false);
        }
    }

    public void onAddResult(Event event) {
        TestSamplesPK id = (TestSamplesPK) sampleDataListView.getSelectedItem().getValue();
        sampleService.addSampleResult(id, result.getText());
        setInfo();
    }

    public void onRemoveSample(Event event) {
        try {
            int status = Messagebox.show("Are you sure to remove samples?", "Question", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION, new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    if (((Integer) event.getData()).intValue() == Messagebox.YES) {
                        System.out.println("Enter remove method");
                        Iterator it = sampleDataListView.getSelectedItems().iterator();
                        System.out.println(sampleDataListView.getSelectedItems().size());
                        while (it.hasNext()) {
                            Listitem li = (Listitem) it.next();
                            TestSamplesPK Id = (TestSamplesPK) li.getValue();
                            sampleService.removeTestSampleForever(Id);
                            System.out.println("The test sample has been deleted");
                        }
                        doFilter();
                        return;
                    } else {
                        System.out.println("Messagebox.No selected!");
                        return;
                    }
                }
            });
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void onEditSample(Event event) {
        try {
            Map params = new HashMap();
            TestSamplesPK id = (TestSamplesPK) sampleDataListView.getSelectedItem().getValue();
            params.put("TestSample", sampleService.findTestSample(id));
            System.out.println("Edit sample of patient: " + ((TestSamples) params.get("TestSample")).getPatientFullName());
            Window win = (Window) Executions.createComponents(
                    "/WEB-INF/zul/pages/laboratory/EditSample.zul", null, params);
            win.setMaximizable(true);
            win.setClosable(true);
            win.doModal();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
}
